package com.txsj.scan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.GoodsInfo;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Created by WangChuangJun on 2018/11/13
 * @Describe
 */
public class InventoryAdapter extends RecyclerView.Adapter<InventoryAdapter.InventoryViewHolder> {

    private List<GoodsInfo> list;
    private Context context;

    public InventoryAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }

    public void setData(List<GoodsInfo> epcGoodsInfos) {
        list.clear();
        list.addAll(epcGoodsInfos);
        notifyDataSetChanged();
    }

    public void updateData(GoodsInfo epcGoodsInfo) {
        int pos = list.indexOf(epcGoodsInfo);
        notifyItemChanged(pos);
    }

    public void updateData(int position, long count) {
        list.get(position).setCount(count);
        notifyItemChanged(position);
    }

    public List<GoodsInfo> getList() {
        return list;
    }

    public void removeData(int position) {
        list.remove(position);
        notifyDataSetChanged();
    }

    ;

    /**
     * 获取单条数据
     *
     * @param position
     */
    public GoodsInfo getInventoryBean(int position) {

        if (position < list.size()) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public InventoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new InventoryViewHolder(LayoutInflater.from(context).inflate(R.layout.item_inventory, parent, false));
    }

    @Override
    public void onBindViewHolder(InventoryViewHolder holder, final int position) {
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
//        GlideImgManager.getLoadRoundImg(context, list.get(position).getGoods_img(), holder.iv_goods_pic);
        holder.tv_goods_name.setText(list.get(position).getGoods_name());
        holder.tv_goods_num.setText(list.get(position).getGoods_num());
        holder.tv_inventory_count.setText("盘点数量：" + list.get(position).getCount());
        holder.tv_current_count.setText("系统数量：" + list.get(position).getInventory_keep());
        float win_loss_price = (float) ((list.get(position).getCount() - list.get(position).getInventory_keep()) * Float.valueOf((decimalFormat.format(list.get(position).getUnit_price()))));
        if (win_loss_price >= 0.0) {
            holder.tv_inventory_price.setText("盘盈金额：" + decimalFormat.format(win_loss_price));
            holder.tv_inventory_price.setTextColor(context.getResources().getColor(R.color.color_win));
        } else {
            holder.tv_inventory_price.setText("盘亏金额：" + decimalFormat.format(win_loss_price));
            holder.tv_inventory_price.setTextColor(context.getResources().getColor(R.color.red));
        }
        holder.iv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != listener) listener.onDelete(position);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != listener) listener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class InventoryViewHolder extends RecyclerView.ViewHolder {
        TextView tv_inventory_count, tv_goods_name, tv_current_count, tv_inventory_price;
        ImageView iv_goods_pic, iv_del;
        TextView tv_goods_num; //条码

        public InventoryViewHolder(View itemView) {
            super(itemView);
            tv_inventory_count = (TextView) itemView.findViewById(R.id.tv_inventory_count);
            tv_goods_name = (TextView) itemView.findViewById(R.id.tv_goods_name);
            tv_goods_num = (TextView) itemView.findViewById(R.id.tv_goods_num);
            tv_inventory_price = (TextView) itemView.findViewById(R.id.tv_inventory_price);
            tv_current_count = (TextView) itemView.findViewById(R.id.tv_current_count);
            iv_del = (ImageView) itemView.findViewById(R.id.iv_del);
            iv_goods_pic = (ImageView) itemView.findViewById(R.id.iv_goods_pic);
        }
    }

    private OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int pisition);

        void onDelete(int position);
    }
}
